﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SharePointSocialNetworking.Webparts.TwitterTrendsWebPart
{
    [ToolboxItemAttribute(false)]
    public class TwitterTrendsWebPart : WebPart
    {
        #region private and properties
        [DefaultValue("SharePoint")]
        public string HashTag { get; set; }
        [DefaultValue("15")]
        public string NumOfTweets { get; set; }
        #endregion

        #region Constructor
        public TwitterTrendsWebPart()
        {
            this.ChromeType = PartChromeType.None;
            this.Width = Unit.Pixel(218);
            this.Height = Unit.Pixel(368);
        }
        #endregion

        #region overrides
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(@"<div id=""silverlightControlHost"">");
            writer.Write("<object data=\"data:application/x-silverlight-2,\" type=\"application/x-silverlight-2\" width=\"218px\" height=\"358px\">");
            writer.Write("<param name=\"source\" value=\"{0}/XAPS/TwitterTrendsSilverlightWebPart.xap\"/>",SPContext.Current.Site.Url);
            writer.Write(@"<param name=""onError"" value=""onSilverlightError"" />");
            writer.Write(@"<param name=""background"" value=""white"" />");
            writer.Write(@"<param name=""minRuntimeVersion"" value=""4.0.50826.0"" />");
            writer.Write(@"<param name=""windowLess"" value=""true""/>");
            writer.Write("<param name=\"initParams\" value=\"MS.SP.url={0},Hashtag={1},Tweets={2}\"/>", SPContext.Current.Web.Url, HashTag, NumOfTweets);
            writer.Write(@"<param name=""autoUpgrade"" value=""true"" />");
            writer.Write(@"<a href=""http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0"" style=""text-decoration:none"">");
            writer.Write(@"<img src=""http://go.microsoft.com/fwlink/?LinkId=161376"" alt=""Get Microsoft Silverlight"" style=""border-style:none""/></a>");
            writer.Write(@"</object><iframe id=""_sl_historyFrame"" style=""visibility:hidden;height:0px;width:0px;border:0px""></iframe></div>");
        }

        public override EditorPartCollection CreateEditorParts()
        {
            return new EditorPartCollection(new EditorPart[] { new TwitterTrendsEditorPart() });
        }
        #endregion
    }
}
